package com.yeyks.exception;

import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.Ajax.AjaxResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@ControllerAdvice
@Slf4j
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResponseBody handle(Exception e) {
        if (e instanceof ServiceException) {
            ServiceException serviceException = (ServiceException) e;
            if (serviceException.getCode() == null) {
                return AjaxResponseBody.failed(serviceException.getMessage());
            } else {
                return AjaxResponseBody.failed(serviceException.getCode(), serviceException.getMessage());
            }
        } else {
            log.error("服务器异常错误{}", e.getLocalizedMessage());
            e.printStackTrace();
            return AjaxResponseBody.failed("服务器异常");
        }
    }
}
